//
//  ViewController.m
//  11-30date
//
//  Created by mac on 15-11-30.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "ViewController.h"
#import "inputViewController.h"
@interface ViewController ()<UITableViewDataSource,UIAlertViewDelegate,Keep>
{
    NSMutableArray *names;
    NSMutableArray *tels;
    NSIndexPath *no;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"电话簿";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    names = @[
             @"张三",
             @"李四"
             ].mutableCopy;
    tels = @[
             @"1581696358",
             @"1685658146"
             ].mutableCopy;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(onEdit:)];
    self.tableView.allowsSelectionDuringEditing = YES;
}

- (void)onEdit:(UIBarButtonItem *)sender{
    self.tableView.editing = !self.tableView.editing;
    sender.title = self.tableView.editing ? @"完成" : @"编辑";
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tableView.editing ? names.count + 1 : names.count;
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    if (indexPath.row < names.count) {
        cell.textLabel.text  = names[indexPath.row];
        cell.detailTextLabel.text = tels[indexPath.row];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.row < names.count ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    inputViewController * con = [[inputViewController alloc]initWithStyle:UITableViewStyleGrouped];
    if (indexPath.row < names.count) {
        con.tel = tels[indexPath.row];
        con.name = names[indexPath.row];
    }else{
        [names addObject:@""];
        [tels addObject:@""];
    }
    no = indexPath;
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];
    if (tableView.isEditing) {
        [tableView setEditing:NO animated:YES];
    }
}

- (void)keepFinishedWithName:(NSString *)name tel:(NSString *)tel{
    NSIndexPath * indexPath = no;
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    names[indexPath.row] = name;
    tels[indexPath.row] = tel;
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleInsert) {
    }else{
        [names removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
